propsage.namespace('modules.admin.system');

modules.admin.system.numbering = (function($, ui) {
	function initEventHandlers() {
		$('input.numbering-method').on('change', onNumberingMethodChange);
		$('.other-settings input[type="checkbox"]').on('change', onOtherSettingsCheckboxChange);
		$('.section.numbering').find('input, select').on('change keydown', onSettingsChange);
		$('.numbering-format').on('keydown change', onNumberingFormatKeydown);
		$('.numbering-zerofill-length').on('keydown change', onZeroFillLengthChange);
	}
	
	function onZeroFillLengthChange() {
		var $input = $(this);
		
		setTimeout(function() {
			var zerofillLength = $input.autoNumeric('get');
			
			var vMax = '9';
			for (var i = 0; i < zerofillLength - 1; i++) {
				vMax += '9';
			}
			vMax = parseInt(vMax);
			
			var currentStartFrom = $('#DocumentNumbering_start_from_number').autoNumeric('get');
			
			if (currentStartFrom > vMax) {
				$('#DocumentNumbering_start_from_number').autoNumeric('set', 1);
			}
			$('#DocumentNumbering_start_from_number').autoNumeric('update', {'vMax': vMax});			
		}, 5);
	}
	
	function onNumberingFormatKeydown(e) {
		$(this).closest('.section.numbering').find('.numbering-method-custom').prop('checked', true);
	}
	
	function onNumberingMethodChange(e) {
		var $section = $(this).closest('.section');
		var $numberingFormat = $section.find('.numbering-format');
		
		var numberingMethod = $(this).val();		
		var numberingFormat = $numberingFormat.val();
		switch(numberingMethod) {
			case 'default':
				numberingFormat = '%autonum%';
				
				$section.find('.restart-numbering').prop('checked', false);
				$section.find('.restart-numbering-type').prop('disabled', true);
				break;
			case 'yyyymm':
				numberingFormat = '%yyyy%/%mm%/%autonum%';
				break;
			case 'yyyy':
				numberingFormat = '%yyyy%/%autonum%';
			case 'custom':
				break;
		}
		
		$numberingFormat.val(numberingFormat);
		
		// update preview
		updatePreview($section.find('.numbering-format-example'));
	}
	
	function onSettingsChange(e) {
		var $section = $(this).closest('.section');
		setTimeout(function() {
			updatePreview($section.find('.numbering-format-example'));
		}, 5);
	}
	
	function updatePreview($element) {
		var $section = $element.closest('.section');
		var numberingFormat = $section.find('.numbering-format').val();
		var isZeroFilled = $section.find('.numbering-zerofill').is(':checked');
		var zeroFillAmt = parseInt($section.find('.numbering-zerofill-length').val());
		
		
		if (!isZeroFilled || isNaN(zeroFillAmt)) {
			zeroFillAmt = 0;
		}
		
		var formatted = getNumberingFormatPreview(numberingFormat, zeroFillAmt);
		
		$element.text(formatted);
	}
	
	function getNumberingFormatPreview(numberingFormat, zerofill) {
		zerofill = zerofill || 0;
		var currentTime = new Date();
		
		var num = 1;
		var isStartFromEnabled = $('#DocumentNumberingStartFrom').is(':checked');
		if (isStartFromEnabled) {
			num = parseInt($('#DocumentNumbering_start_from_number').val())
		}
		
		var autonum = pad(num, zerofill);		
		var tokens = {
			'%yyyy%': currentTime.getFullYear(),
			'%yy%': currentTime.getFullYear().toString().substring(2, 4),
			'%mm%': ("0" + (currentTime.getMonth() + 1)).slice(-2),
			'%autonum%': autonum,
			'%clientrole_code%': 'B'
		};
		
		var formatted = numberingFormat;
		$.each(tokens, function(token, value) {
			formatted = formatted.replace(token, value);
		});
		
		return formatted;
	}
	
	function pad(number, length) {
		var str = '' + number;
		while (str.length < length) {
			str = '0' + str;
		}
   
		return str;

	}
	
	function onOtherSettingsCheckboxChange(e) {
		$(this).closest('tr').find('input[type="text"], select').prop('disabled', !$(this).is(':checked'));
		
		updatePreview($(this).closest('.section.numbering').find('.numbering-format-example'));
	}
	
	$(document).ready(function() {
		initEventHandlers();
		
		updatePreview($('.numbering-format-example'));
	});
}(jQuery, propsage.ui));